/*****************************************************************************
 * file  : systask.c
 * creat : apleilx
 * data  : 2017/11/05
 *******************************************************************************/
#define _GUI_MAIN_MODULE_
#include "os_obj.h"
#include "pincfg.h"
#include "dataproc.h"
#include "gui_main.h"
#include "string.h"
#include "lcd_yl.h"
#include "bsp_api.h"
#include "str_ext.h"
#include "par\par_tab.h"

void img_check(void);
void spim_sw_spi_init(void);

int oled_init(void);
void gui_dbg(void);
void lcd_data_write_ext(char page, uu8 *pLineUp, uu8 *pLineDn, uu8 offset);
void lcd_data_write(char page, uint8_t *pdata);
void lcd_hal_init(void);
const uu8 * lcd_yl_gb2312_cn_get(uu32 mem_offset,  uu16 font_x);

extern par_manager_type par_set;
lcd_yl_def_type lcd_obj;
extern unsigned char LcdRam[];

/*****************************************************************************
 * @brief   extern exe list.
 *****************************************************************************/
FUN_LIST_CREAT(GUI);
FUN_LIST_CREAT(KEY_S1);
FUN_LIST_CREAT(KEY_S2);
FUN_LIST_CREAT(KEY_S3);
FUN_LIST_CREAT(KEY_S4);

/*****************************************************************************
 *
 * @brief   storage task.
 *
 * @param   none
 *
 * @return  none
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void gui_task(void const *argument)
{
	uint16_t row_start, row_size;

	osDelay(ms_ticks(20));
	
	lcd_hal_init();

	lcd_yl_init(&lcd_obj, LcdRam, 4*64, 24 * 8);

	lcd_obj.gb2312_cn_get = lcd_yl_gb2312_cn_get;
	
	pulse_init(&gui_st.pulse, app_tick_get());

	lcd_yl_origin_set(0, 16);

	gui_st.move_site_x = 0;
	lcd_yl_font_set(32, 32, lcd_yl_font_16_32);

	lcd_yl_ram_fill(0xFF);
	lcd_yl_anti();

	while (1)
	{
		lcd_yl_origin_set(32, 16);
		pulse_creat(&gui_st.pulse, app_tick_get());
		osSignalWait(0, ms_ticks(2));

		lcd_yl_poll(32, 2, 8, lcd_data_write);
		lcd_yl_space_set(4);
		lcd_yl_half_en();

		lcd_yl_font_set(24, 24, lcd_yl_font_12_24);
		lcd_yl_alain_puts(64, 32, "OLED", TEXT_X_MID | TEXT_Y_MID | TEXT_SQ(2, 7, 0) | TEXT_LINE(2, 0));
		lcd_yl_origin_set(32 + 128, 16);

		if (gui_st.move_mode)
		{
			break;
		}
		break;
	}

	lcd_yl_ram_fill(0);
	lcd_yl_space_set(1);
	lcd_yl_origin_set(32, 16);
	gui_st.move_site_x = 32;
	gui_st.move_site_y = 16;

	while (1)
	{
		if (gui_st.move_en /*&& (lcd_yl_update_cnt_get() == 7)*/)
		{
			static uint16_t mvcnt;

			mvcnt++;
			if (gui_st.move_mode)
			{
				gui_st.move_site_x++;
			}
			else
			{
				gui_st.move_site_x--;
			}

			if (gui_st.move_site_x < 0)
				gui_st.move_site_x = 0;

			if (gui_st.move_site_x > 128)
				gui_st.move_site_x = 128;

			if (mvcnt & 0x20)
				gui_st.move_site_x = mvcnt & 0x1F;
			else
				gui_st.move_site_x = 0x1F - (mvcnt & 0x1F);

			if (gui_st.move_site_y < 0)
				gui_st.move_site_y = 0;

			if (mvcnt & 0x40)
				gui_st.move_site_y = mvcnt & 0x3F;
			else
				gui_st.move_site_y = 0x3F - (mvcnt & 0x3F);

			if (gui_st.notice_mode)
			{
				gui_st.move_site_x = 32;
				gui_st.move_site_y = 16;
			}
		}

		pulse_creat(&gui_st.pulse, app_tick_get());

		osSignalWait(0, ms_ticks(25));

		lcd_yl_poll_ext(gui_st.move_site_x, gui_st.move_site_y, 8, lcd_data_write_ext);
		while (lcd_yl_update_cnt_get())
		{
			lcd_yl_poll_ext(gui_st.move_site_x, gui_st.move_site_y, 8, lcd_data_write_ext);
		}

		lcd_yl_normal();
		lcd_yl_square_fill(0, 0, 127, 63);
		lcd_yl_anti();

		if (par_set.st_set)
		{
			char buff[32];
			int len;

			lcd_yl_normal();
			lcd_yl_square_fill(0, 0, 127, 63);
			gui_st.move_site_x = 32;
			gui_st.move_site_y = 16;
			lcd_yl_font_set(16, 16, lcd_yl_font_8_16);
			lcd_yl_anti();

			lcd_yl_alain_puts(64, 16, par_attr_tab[par_set.par_num].name, TEXT_X_MID);

			len = i32_to_string(buff, par_set.par_val.ival, ((8-(par_set.par_attr & PAR_ATTR_POINT_MSK))<<4) | (par_set.par_attr & PAR_ATTR_POINT_MSK), 0);
			
			lcd_yl_puts(64 - 8 * 8 / 2, 32, buff);
			lcd_yl_normal();
			if(par_set.par_attr & PAR_ATTR_POINT_MSK)
			{
				uu08 pos = par_set.par_attr & PAR_ATTR_POINT_MSK;
				
				if(gui_st.edit_shift < pos)
				{
					lcd_yl_put_char(64 - 8 * 8 / 2 + 9 * (len - 2 - gui_st.edit_shift) + 4, 32, buff[len - 1 - gui_st.edit_shift]);
				}
				else
				{
					lcd_yl_put_char(64 - 8 * 8 / 2 + 9 * (len - 2 - gui_st.edit_shift), 32, buff[len - 2 - gui_st.edit_shift]);
				}
			}
			else
			{
				lcd_yl_put_char(64 - 8 * 8 / 2 + 9 * (len - 1 - gui_st.edit_shift), 32, buff[len - 1 - gui_st.edit_shift]);
			}
		}
		else if (cap_period_get())
		{
			char buff[32];
			gui_st.move_site_x = 32;
			gui_st.move_site_y = 16;
			lcd_yl_font_set(16, 16, lcd_yl_font_8_16);
			sprintf(buff, "peri: %5dus", cap_period_get());
			lcd_yl_alain_puts(64, 0, buff, TEXT_X_MID);
			sprintf(buff, "duty: %5dus", cap_duty_get());
			lcd_yl_alain_puts(64, 16, buff, TEXT_X_MID);
			sprintf(buff, "cnt:%9d", cap_pulse_get());
			lcd_yl_alain_puts(64, 32, buff, TEXT_X_MID);
		}
		else if (gui_st.notice_mode)
		{
			char buff[32];
			ii16 index = 0;

			lcd_yl_font_set(16, 16, lcd_yl_font_8_16);
			for (int cnt = 0; cnt < 4; cnt++)
			{
				str_get_line((void *)&gui_st.notice_str[index], buff);

				lcd_yl_alain_puts(64, cnt * 16, buff, TEXT_X_MID);
				index = str_site_next_line((void *)&gui_st.notice_str, index);
				if (index < 0)
					break;
			}
		}
		else
		{
			lcd_yl_normal();
			lcd_yl_square_fill(0, 0, 127, 63);
			lcd_yl_anti();
			lcd_yl_font_set(32, 32, lcd_yl_font_16_32);
			lcd_yl_alain_puts(64, 32, "CHINA", TEXT_X_MID | TEXT_Y_MID | TEXT_SQ(2, 6, 0) | TEXT_LINE(1, 0));

			fun_list_run_once_pure(GUI);
		}

		if (gui_st.page_reg >= 0)
		{
			gui_st.page_now = gui_st.page_reg;
			gui_st.page_reg = -1;
		}

		if (gui_st.pulse.pulse_100ms)
		{
			gui_st.page_keep_time_cnt++;
		}

		if (gui_st.page_old != gui_st.page_now)
		{
			gui_st.page_keep_time_cnt = 0;
		}

		gui_st.page_old = gui_st.page_now;
		gui_st.gui_cnt++;
	}
}

void gui_notice(const char *str)
{
	gui_st.notice_mode = 1;
	copy_str(str, gui_st.notice_str, 128);
}
